#include "MapSelector.h"
#include "General Utils.hpp"


MapSelector::MapSelector()
{	  
	//for(int i = 0; i < MapManager::GetAllMaps().size(); ++i)
	//	mMapData.push_back(MapManager::GetAllMaps()[i]);
	Log->AddMessage("Constructing map selector");

	mMapData = MapManager::GetAllMaps();

	for(unsigned int i = 0; i < mMapData.size(); ++i)
	{
		DataLoader->LoadData(mMapData[i]);
		mMinimaps.resize(mMinimaps.size() + 1);
		mCreatedMinimaps.push_back(false);
		//mMinimaps[i].Init(mMapData[i].MapPath.c_str());
		mMinimaps[i].SetLocation(DataLoader->WINDOW_WIDTH - mMinimaps[i].GetDimensions().w - 10,
			DataLoader->WINDOW_HEIGHT - mMinimaps[i].GetDimensions().h - 10);
		mActiveMinimaps.push_back(false);

		//if(i == 0)
		//	mActiveMinimaps[i] = true;
	}


	Lua->RunScript("Scripts\\UI\\DisableAll.lua");
	GUI->RegisterPanel(Key->GetPanelIDs()->MapSelector_NotRec);
	GUI->ActivatePanel();
	GUI->RegisterPanel(Key->GetPanelIDs()->MapSelector_Screen);
	GUI->ActivatePanel();


	for(unsigned int i = 0; i < mMapData.size(); ++i)
	{
		GUI->AddButton(mMapData[i].ID, MapManager::GetNormalImage(mMapData[i].MapName), MapManager::GetMouseoverImage(mMapData[i].MapName));
		GUI->AddText(mMapData[i].MapName.c_str(), Key->GetColor("217, 217, 217"), "onuava");
	}

	GUI->ActivatePanel();
	Log->AddMessage("Exiting Map Selector constructor");
}
MapSelector::~MapSelector()
{
	for(unsigned int i = 0; i < mMinimaps.size(); ++i)
		mMinimaps[i].Delete();

	GUI->ClearButtons();
	GUI->ClearText();

}

void MapSelector::handle_events()
{
    while( SDL_PollEvent( &event ) )
    {
        // Mouse offsets
        int x = 0, y = 0;

        // On mouse motion
        if(event.type == SDL_MOUSEMOTION)
        {
		
			x = event.motion.x;
			y = event.motion.y;


			GUI->RegisterPanel(Key->GetPanelIDs()->MapSelector_NotRec);
			int ID = GUI->GetButtonID(x, y);
			
			GUI->SwapButton(true);
			GUI->SwapButton(false, ID);


			GUI->RegisterPanel(Key->GetPanelIDs()->MapSelector_Screen);
			ID = GUI->GetButtonID(x, y);

			GUI->SwapButton(true);
			GUI->SwapButton(false, ID);


			for(int i = 0; i < mMapData.size(); ++i)
				if(ID == mMapData[i].ID)
				{
					
					for(unsigned int j = 0; j < mActiveMinimaps.size(); ++j)
						mActiveMinimaps[j] = false;

					mActiveMinimaps[i] = true;


					if(mCreatedMinimaps[i] == false)
					{
						DataLoader->LoadData(mMapData[i]);
						string mapDataPath = mMapData[i].MapPath;
						mMinimaps[i].Load(mMapData[i].MapPath);
						mCreatedMinimaps[i] = true;
					}
				}
			
        }

        // Mouse press
        if(event.type == SDL_MOUSEBUTTONDOWN)
        {
            // Get the mouse offsets
            x = event.button.x;
            y = event.button.y;

            // Left button
            if(event.button.button == SDL_BUTTON_LEFT)
            {
				int ID = GUI->GetButtonID(x, y);

				for(unsigned int i = 0; i < mMapData.size(); ++i)
					if(ID == mMapData[i].ID)
					{
						DataLoader->LoadData(mMapData[i]);
						//Lua->RunScript(mMapData[i].MapScript.c_str());
						set_next_state(STATE_GAMEPLAY);
					}

				if(ID == Key->GetPanelIDs()->MapSelector_Close)
					set_next_state(STATE_MAINMENU);	// Change this so the map selector will close instead
                
			}
        }    
                
        //If the user has Xed out the window
        if( event.type == SDL_QUIT )
        {
            //Quit the program
            set_next_state( STATE_EXIT );
        }
    }
}
void MapSelector::logic()
{
}
void MapSelector::render()
{
	/*DrawPrimitives->DrawRectangle(90, 90, 105, WH-90, customColor);
	DrawPrimitives->DrawRectangle(WW-105, 90, WW-90, WH-90, customColor);
	DrawPrimitives->DrawRectangle(105, 90, WW-105, 105, customColor);
	DrawPrimitives->DrawRectangle(105, WH-105, WW-105, WH-90, customColor);*/

	GUI->Render();

	// Minimaps
	for(unsigned int i = 0; i < mActiveMinimaps.size(); ++i)
		if(mActiveMinimaps[i] == true)
			mMinimaps[i].RenderMinimap();

}

